package com.ljn.service.imp;

import com.ljn.core.dto.Student;
import com.ljn.core.dto.common.exceptions.UserIsExistedException;
import com.ljn.core.dto.common.utils.UUIDUtils;
import com.ljn.core.dto.common.utils.ency.MD5EncryptUtil;
import com.ljn.mapper.StudentMapper;
import com.ljn.service.StudentService;
import org.springframework.transaction.annotation.Transactional;

import javax.inject.Inject;
import javax.inject.Named;

/**
 * Created by liujunnan on 2017/11/3.
 */
@Named
public class StudentServiceImpl implements StudentService {

    @Inject
    StudentMapper studentMapper;

    private Student findByUsername(String userName) {
        Student student = studentMapper.findUserByUsername(userName);
        return studentMapper.findUserByUsername(userName);
    }


    @Transactional
    public Long register(Student student) {
        Student result = this.findByUsername(student.getUserName());
        if (result != null) {
            throw new UserIsExistedException("用户存在");
        } else {
            student.setUserId(UUIDUtils.get32UUIDAsLowerCase());
            student.setPassword(MD5EncryptUtil.encryptPassword(student.getPassword(), student.getUserId()));
            studentMapper.insertUser(student);
            studentMapper.insertStudent(student);
            return student.getPhone();
        }
    }

    @Override
    public Student selectStudent(String userId) {
        Student student = studentMapper.findStudentById(userId);
        return student;
    }


    @Transactional
    public boolean changeUserInfo(Student student) {

       boolean i = this.studentMapper.updateByPrimaryKeySelective(student) > 0 ? true:false;
       boolean t = this.studentMapper.updateByPrimaryKeySelectiveUser(student) >0 ? true:false;
       return  i&&t ;
    }


}
